/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.support.v4.app.FragmentManager;
import android.widget.GridView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.activity.NDNiudanActivity;
import com.simon.mengkou.ui.adapter.NDOptionsAdapter;
import com.simon.mengkou.ui.base.BaseNiudanFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/6.
 * desc    :扭蛋列表界面
 */
public class NDOptionsFragment extends BaseNiudanFragment
                implements PullToRefreshBase.OnRefreshListener2<GridView>{
    @Bind(R.id.options_id_grid) PullToRefreshGridView mPtrOptions;

    private NDOptionsAdapter mAdapter;
    private int mPage = 0;

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_options);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());
        setDoorVisible(true);
        openDoor(null, 500);

        mAdapter = new NDOptionsAdapter(mActivity);
        mPtrOptions.getRefreshableView().setAdapter(mAdapter);
        mPtrOptions.getLoadingLayoutProxy().setLoadingDrawable(null);
        mPtrOptions.setOnRefreshListener(this);

        getNiudanList(mPage);
    }


    /**
     * 获取我的扭蛋列表
     */
    private void getNiudanList(int page) {
        AgnettyFuture future = OuerApplication.mOuerFuture.getNiudanList(
                page, CstOuer.PAGE.DEFAULT_SIZE,
                new OuerFutureListener(mActivity) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        List<Niudan> datas = (List<Niudan>) result.getAttach();
                        mPtrOptions.onRefreshComplete();

                        if(UtilList.isNotEmpty(datas)) {
                            mAdapter.addList(datas);
                            mPage++;
                            if(UtilList.getCount(datas) < CstOuer.PAGE.DEFAULT_SIZE) {
                                mPtrOptions.setMode(PullToRefreshBase.Mode.DISABLED);
                            }
                        } else {
                            mPtrOptions.setMode(PullToRefreshBase.Mode.DISABLED);
                        }

                    }
                });

        attachDestroyFutures(future);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
        //下拉刷新
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
        //上拉加载更多
        getNiudanList(mPage);
    }



    @Override
    public void onBackPressed() {
        final FragmentManager fm = mActivity.getSupportFragmentManager();
        if(fm.getBackStackEntryCount() == 0){
            closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
                @Override
                public void onClose() {
                    finish();
                }
            });

        }else{
            closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
                @Override
                public void onClose() {
                    fm.popBackStack();
                    openDoor(null);
                }
            });

        }
    }
}
